<?php
// +----------------------------------------------------------------------
// | ThinkSNS
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://www.thinksns.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Daniel Yang <desheng.young@gmail.com>
// +----------------------------------------------------------------------
//

/**
 +------------------------------------------------------------------------------
 * MailService 邮件发送服务
 * 实现邮件的STMP发送，支持大批量发送
 * 为系统通知和提醒提供支持
 +------------------------------------------------------------------------------
 * @category	addons
 * @package		addons
 * @subpackage  services
 * @author		Daniel Yang <desheng.young@gmail.com>
 * @version		$Id$
 +------------------------------------------------------------------------------
 */

class MailService extends Service {
	
	private $option = array();
	private $dao    = array();
	private	$close	= false;	//关闭邮件发送功能

	public function __construct() {
		$this->init();
	}
	
	public function init() {
		include_once(SITE_PATH.'/addons/libs/phpmailer/class.phpmailer.php');
		include_once(SITE_PATH.'/addons/libs/phpmailer/class.pop3.php');
		include_once(SITE_PATH.'/addons/libs/phpmailer/class.smtp.php');
		
		$this->dao = model('Xdata');
		$email = $this->dao->lget('email');
		$this->option = $email;

		/*
		$this->option = array(
			'sendtype'		=> 'smtp',
			'host'			=> 'smtp.huawei.com',
			'port'			=> '25',
			'ssl'			=> false,
			'account'		=> 'w00135495',
			'password'		=> 'Aniceday1027',
			'sender'		=> 'KMP(no-reply)',
			'sender_account'=> 'kmp_no_replay@notesmail.huawei.com',
			'reply_account'	=> 'kmp_allow_reply@notesmail.huawei.com'  //[暂不使用]
		);
		*/
	}

	public function getOption() {
		return $this->option;
	}
	
	//单封邮件直接发送，多封邮件收件人超过10封上限的，加入队列发送
	public function send_email( $sendto_email, $subject, $body, $senderInfo = '' ) {
		//return true;
		//少于N个收件人，直接发送
		if( ( intval($this->option['limit']) > 0 ) && ( count($sendto_email) < $this->option['limit'] ) ) {
			return $this->send($sendto_email, $subject, $body, $senderInfo);
			
		}else{
			return model('Xmail')->addToList($sendto_email, $subject, $body, 1, $senderInfo);	
		}
	}
	
	//定时邮件列表形式发送
	public function auto_send_email( $sendto_email, $subject, $body, $senderInfo = '' ) {
		//return true;
		if(!is_array($sendto_email)) {
			return $this->send($sendto_email, $subject, $body, $senderInfo);
		}

		$offset = 0;
		$length = $this->option['limit'];
		$res    = true;
		while(1) {
			$sendto = array_slice($sendto_email, $offset, $length);
			if (empty($sendto)) {
				break;
			}
			$res = $res && $this->send($sendto, $subject, $body, $senderInfo);
			if($res == false) {
				break;
			}
			$offset += $length;
		}
		return $res;
	}

	//实际的邮件发送函数
	public function send( $sendto_email, $subject, $body, $senderInfo = '' ) {

		//关闭邮件发送
		if($this->close){
			return false;
		}

        $mail = new PHPMailer();
		if(empty($senderInfo)) {
			$sender_name  = $this->option['sender'];
			$sender_email = $this->option['sender_account'];
		}else {
			$sender_name = $senderInfo['name'];
			$sender_email = $senderInfo['email'];
		}

		if($this->option['sendtype'] =='smtp'){
			$mail->Mailer = "smtp";
			$mail->Host	= $this->option['host'];	// sets GMAIL as the SMTP server
			$mail->Port	= $this->option['port'];	// set the SMTP port

			if((bool)$this->option['ssl']) {
				$mail->SMTPSecure	=	"ssl";		// sets the prefix to the servier  tls,ssl
			}
			
			if(empty($this->option['account'])) {
				$mail->SMTPAuth = false;					 // turn off SMTP authentication
			}else {
				$mail->SMTPAuth = true;						 // turn on SMTP authentication
				$mail->Username = $this->option['account'];	 // SMTP username
				$mail->Password = $this->option['password']; // SMTP password
			}
		}
//		elseif($sendmail_type=='sendmail'){
//
//			$mail->Mailer	=	'sendmail';
//			$mail->Sendmail	=	'';
//		}else {
//
//			$mail->Mailer	=	'mail';
//		}

		$mail->FromName	= $sender_name;  // 发件人姓名
		$mail->From		= $sender_email; // 发件人邮箱


		$mail->CharSet	= "UTF-8"; // 这里指定字符集！
		$mail->Encoding	= "base64";

		if(is_array($sendto_email)) {
			foreach($sendto_email as $v){
				$mail->AddAddress($v);
			}
		}else{
			$mail->AddAddress($sendto_email);
		}

		if(!empty($senderInfo)) {
			$mail->AddReplyTo($sender_email, $sender_name); //收件人邮箱和姓名
		}

		//以HTML方式发送
		$mail->IsHTML(true); // send as HTML
		// 邮件主题
		$mail->Subject	 =	$subject;
		// 邮件内容
		$mail->Body		 =	'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'."\n\t".$body;
		$mail->AltBody	 =	"text/html";
		$mail->SMTPDebug =	false;
		return $mail->Send();
	}
	
	public function run() {

	}

	public function _start() {

	}

	public function _stop() {

	}

	public function _install() {

	}

	public function _uninstall() {

	}
}
?>